const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    imgSrc: SERVICE.imgUrl,
    showNoData: false,
    showNoData2: false,
    shareData:{
      show: false,
      quit: "shareQuit",
      share: "Share"
    },
    showNavItem: 0
  },

  onLoad(){
    // this.getMyRentalList();
  },
  onShow(){
    this.getMyRentalList();
    this.getMyDemandRentList();
  },

  /*分享*/
  onShareAppMessage(){

  },

  getNavItem(e){
    let index = e.currentTarget.dataset.index;
    this.setData({
      showNavItem: index
    });
    if(index == 0){
      this.getMyRentalList();
    }else if(index == 1){
      this.getMyDemandRentList();
    }
  },

  /*分享按钮*/ 
  share(e){
    console.log(e);
    let id  = e.currentTarget.dataset.id;
    this.data.shareData.show = true;
    this.setData({
      shareData: this.data.shareData,
      id: id
    });
  },

  /*分享退出按钮*/ 
  shareQuit(){
    this.data.shareData.show = false;
    this.setData({
      shareData: this.data.shareData
    });
  },
  
  /*删除事件*/ 
  Delete(e){
    let _this = this, id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "提示",
      content: "确认要删除吗",
      success: function(res){
        if(res.confirm){
          _this.deleteRental(id);
        }
      }
    })
  },

  Delete2(e) {
    let _this = this, id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "提示",
      content: "确认要删除吗",
      success: function (res) {
        if (res.confirm) {
          _this.delDemandRental(id);
        }
      }
    })
  },

  /*上架或下架*/ 
  getPutaway(e) {
    let id = e.currentTarget.dataset.id;
    this.putaway(id);
  },
  getPutaway2(e) {
    let id = e.currentTarget.dataset.id;
    this.putAwayDemand(id);
  },

  /**
   * @explain getMyRentalList 获取我的发布 出租
   * @param thirdCookie 用户标识
   * */ 
  getMyRentalList(){
    let _this = this;
    wx.showLoading({
      title: "加载中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.getMyRentalList,{
      thirdCookie: wx.getStorageSync("thirdCookie")
    },(status,res)=>{
      wx.hideLoading();
      console.log("出租:",res);
      let res_data = res.data;
      if (res_data.status == "success") {
        if (res_data.rentalList.length == 0){
          _this.setData({
            list: res_data.rentalList,
            showNoData: true
          });
        }else{
          _this.setData({
            list: res_data.rentalList
          });
        }
      }else{
        wx.showToast({
          title: "获取信息失败",
          icon: "none",
          mask: true
        });
      }
    });
  },

  /**
   * @explain getMyDemandRentList 获取我的发布 求租
   * @param thirdCookie 用户标识
   * */
  getMyDemandRentList() {
    let _this = this;
    wx.showLoading({
      title: "加载中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.getMyDemandRentList, {
      thirdCookie: wx.getStorageSync("thirdCookie")
    }, (status, res) => {
      wx.hideLoading();
      console.log("求租:", res);
      let res_data = res.data;
      if (res_data.status == "success") {
        if (res_data.myDemandList.length == 0) {
          _this.setData({
            list2: res_data.myDemandList,
            showNoData2: true
          });
        } else {
          _this.setData({
            list2: res_data.myDemandList
          });
        }
      } else {
        wx.showToast({
          title: "获取信息失败",
          icon: "none",
          mask: true
        });
      }
    });
  },
  /**
   * @explain deleteRental 删除指定出租信息
   * @param thirdCookie 用户标识
   * @param rental_id 租赁id
   * */ 
  deleteRental(id){
    let _this = this;
    wx.showLoading({
      title: "删除中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.deleteRental, {
      thirdCookie: wx.getStorageSync("thirdCookie"),
      rental_id: id
    }, (status, res) => {
      wx.hideLoading();
      let res_data = res.data;
      if (res_data.status == "success"){
        _this.getMyRentalList();
      }else{
        wx.showToast({
          title: "删除失败",
          icon: "none",
          mask: true
        });
      }
    }); 
  },
  delDemandRental(id){
    let _this = this;
    wx.showLoading({
      title: "删除中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.delDemandRental, {
      thirdCookie: wx.getStorageSync("thirdCookie"),
      demand_id: id
    }, (status, res) => {
      wx.hideLoading();
      let res_data = res.data;
      if (res_data.status == "success"){
        _this.getMyDemandRentList();
      }else{
        wx.showToast({
          title: "删除失败",
          icon: "none",
          mask: true
        });
      }
    }); 
  },

  /**
   * @exlain  车位租赁上架或下架
   * @param thirdCookie 用户标识
   * @param rental_id 租赁id
   * */ 
  putaway(id){
    let _this = this;
    wx.showLoading({
      title: "加载中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.putaway, {
      thirdCookie: wx.getStorageSync("thirdCookie"),
      rental_id: id
    }, (status, res) => {
      wx.hideLoading();
      if (res.data.status == "success") {
        _this.getMyRentalList();
      } else {
        wx.showToast({
          title: "删除失败",
          icon: "none",
          mask: true
        });
      }
    });
  },
  putAwayDemand(id){
    let _this = this;
    wx.showLoading({
      title: "加载中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.putAwayDemand, {
      thirdCookie: wx.getStorageSync("thirdCookie"),
      demand_id: id
    }, (status, res) => {
      wx.hideLoading();
      if (res.data.status == "success") {
        _this.getMyDemandRentList();
      } else {
        wx.showToast({
          title: "删除失败",
          icon: "none",
          mask: true
        });
      }
    });
  },
  

  /**
   * @explain complie 编辑
   * 
   * */
  complie(e){
    let item = e.currentTarget.dataset.item;
    if (item.rental_genre === "chuzu"){
      wx.navigateTo({
        url: "../lease-release/lease-release?detailId=" + item.rental_id
      });
    }else{
      wx.navigateTo({
        url: "../rent/rent?detailId=" + item.rental_id
      });
    }
  },
  publish(){
    wx.navigateTo({
      url: "../lease-release/lease-release"
    });
  },
  publish2() {
    wx.navigateTo({
      url: "../rent/rent"
    });
  },

  Share() {
    let td = this.data;
    console.log(td.id);
    wx.navigateTo({
      url: "../share-page/share-page?type=0&id=" + td.id,
    })
  }
});